package com.example.mini_notepad.activity;

import com.example.mini_notepad.R;
import com.example.mini_notepad.adapter.SectionsPagerAdapter;
import com.example.mini_notepad.db.NoteDBAdapter;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.res.Resources;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
	
	SectionsPagerAdapter pagerAdapter = null;
	ViewPager viewPager = null;
	NoteDBAdapter adapter;
	static Resources resources;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
		
		final ActionBar actionBar = this.getActionBar();
		// need to refine it.
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
			actionBar.setHomeButtonEnabled(false);
		}
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		viewPager = (ViewPager) findViewById(R.id.main_pager);
		viewPager.setAdapter(pagerAdapter);
		viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
			@Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
		});
		
		for(int i=0; i<pagerAdapter.getCount(); i++){
			actionBar.addTab(
					actionBar.newTab()
					.setText(getPageTitle(i))
					.setTabListener(this));
					
		}
		// construct database
		adapter = new NoteDBAdapter(getBaseContext());
		adapter.copyDB();
		resources = this.getResources();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	private CharSequence getPageTitle(int position){
		switch(position){
			case 0:
				return getResources().getString(R.string.text_title);
			case 1:
				return getResources().getString(R.string.record_title);
			case 2:
				return getResources().getString(R.string.saved_title);
			default:
				return new String("");
		}
	}
	
	public static Resources getResoures(){
		return resources;
	}
}
